package com.zhp.farmerclient.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhp.farmerclient.common.Result;
import com.zhp.farmerclient.entity.Survey;
import com.zhp.farmerclient.mapper.SurveyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SurveyService {
    @Autowired
    public SurveyMapper surveyMapper;

    //意见反馈
    public Result suggest(Survey survey){
        surveyMapper.insert(survey);
        return Result.succ();
    }

    //删除
    public Result delete(int id) {
        surveyMapper.deleteById(id);
        return Result.succ();
    }

    //批量删除
    public Result deletes(List<Integer> ids) {
        surveyMapper.deleteBatchIds(ids);
        return Result.succ();
    }

    //分页显示（查询）
    public Result page(Integer pageNum, Integer pageSize, String name, String content) {
        Page<Survey> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Survey> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        if (!"".equals(content)) {
            queryWrapper.like("content",content);
        }
        queryWrapper.orderByDesc("id");
        IPage<Survey> iPage = surveyMapper.selectPage(page,queryWrapper);
        return Result.succ(iPage.getRecords(),iPage.getTotal());
    }

    //根据用户ID查看用户反馈列表
    public Result check(Integer page, Integer size, Integer id) {
        Page<Survey> surveyPage = new Page<>(page,size);
        QueryWrapper<Survey> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        queryWrapper.eq("user_id",id);
        IPage<Survey> iPage = surveyMapper.selectPage(surveyPage,queryWrapper);
        return Result.succ(iPage.getRecords(),iPage.getTotal());
    }

    // 获取反馈详情
    public Result detail(int id){
        Survey survey = surveyMapper.selectById(id);
        return Result.succ(survey,null);
    }
}
